package com.numberone.master.common.wxpay;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.numberone.master.common.exception.BizException;

import java.util.Calendar;

import org.apache.commons.codec.binary.Base64;

/**
 * Created by zsx on 2017/1/17.
 */
public class Example {
	//微信appid
	public static String WX_APP_ID = "wx825f2c6ff20c43a7";
	
	public static String NO_TIFY_URL = "http://121.196.210.134:8082/wlwks/project/wlwk/notifyOrder.do";

    public static String merchantPrivateKey =
            "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDKJ4LXIq3falGG\n" +
            "a/QhqSEqUxIgSwRNikzJukuqBl5DXhw0B3t5zMQ6tJ2oA/hWf+n108q0i1C2cddV\n" +
            "+pZeUq+mWL5qDrEjoOBdDpUtBb4lqeiKvQfYu9bvNOpCFscJ/45QojfycL3h2wEw\n" +
            "UDw7broRqZyiUwvRhS28F1bena7hItFQ2KNjVs0p9ommf/gJK7Xcl26OcqxQPGr3\n" +
            "4vAgx3TuAqzh2/dl6e5lIcUbEEBfsBh6ez/J1mcrPwfX+6TnErncuvgv38y+Uqk3\n" +
            "u6+r1AnikuO7H7QGJsT1Azw8y37Prtr9lSIeJhZxWljQB4qmsxXyiDHFvCFlpaeS\n" +
            "6r5rvrWrAgMBAAECggEALUp64u5PHD36s+4dzSjSWGQyLHACL8kreQq3BwnUNq2w\n" +
            "DewbOueokyqCI/F3wxF3fm8L/DU2SRnqT8YLyGgoGFHLn4FZKI1TlyW8QFEID/Io\n" +
            "OmUti95RwlgNn+7wewn5fZFKjZO6xw5FDuj7BanCIFDq2edXP4LiA0ELYmbblm7L\n" +
            "mOBCCfuL1ZJDSmrQa6gPyQlGakAR0zs63p/wn6/bS9pPua65AvECq4hhfPUPVVor\n" +
            "fkO1BNfnq5mes77AG3X8jdtbC9UekvmLroMaweV5W3wBjPOAYTvXDpiT4VrxQ80L\n" +
            "3rFnL2cKDcB+rIscotF/FfmYnvygEEyKaGzxgAnYqQKBgQDnmoI/xcAZvdObYYXS\n" +
            "xZyuBLaLP3nANz40DI7rapcqVc5lMRNppwqfIkkLqEhpkIU12r4rCGKzPUjgrmZ4\n" +
            "eyaH8uJgSfaJ748OIhcsFN8iGHBmvotFgZ5SSs9kFaivGGn0ecgpniC8q/QzEF9Z\n" +
            "m/8O6yefUa3UiwnmKO39mswxzwKBgQDfct20Y2o1qShfdsCqLl3bGl//5JeZGeYQ\n" +
            "q0rTZOpFrtMNOw25ZD5+qGNMZzYp3O64JPlvwegXP6hROh7p4qvyPjvRgB87qFo3\n" +
            "aCq1H3PGxKdcWUyGAee5z1z3K8/Ku+JPfw21n1AjsOK/t7XXE+gbnIpRno70AX7j\n" +
            "V+alkL7BZQKBgQCLW3MIAGtIkeoj0ph9bJ6MLxXPz8/oktUA6JWOncJ7nMnmJZNy\n" +
            "vSos+qpNM2Lu8iDVeNt4dUE+bJ/PYeESmbdjkggYvCh2cwj3Dhg3VA+3KlFn4TtY\n" +
            "BVhIV+9WX5nyUZRfoG+KazVzkVwM5csZ1j2wslpTCQi5nTfFn9ckA+LeDQKBgF19\n" +
            "Fb2PirSA3d9zNBD/7UzqsBfEcJkh+Nwojtn7cvlMuAjlGEzk4DDmCwXAeSzyvv5U\n" +
            "mfhkTNLNU4MIqj55FC/6UdTJoke3nGdqMdQuIKadSNielCHNUkKvu9m+fuX3ziws\n" +
            "oHPJACKus3A7UN13+rjyufyENpNovMop0NqcwPLxAoGBAKf051E07Ob54SjuV/jB\n" +
            "KC9uPokVk7YKMvovGFjiA8hj8sKK6Qfim5C96/lV/uCigwKizrEiPrD6/aydh6fZ\n" +
            "mnfU3wbNt+NV7ZCnr9VR9AYhqRinW1iKr7NYXNAEDaqXAeT6rnA/a4XqcSpevFZX\n" +
            "0ehj9pJDp0abqeoUZytHN1tg\n";
            

    public static String platformPublicKey = 
            "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAw99jSFKEQrP1f8Z/3Vt8\n" +
            "FEK9KqIqHtCzd1pLijGR3JmSR7pdg7x7+35GLwtYFVP1EISm4ainQ6p8ozCSp6Co\n" +
            "Q9EG5Ko3DzijjK8ZY8D79HBmo5vr/3nQ/FsUwFA0idB3WSzEXaHj92DklXI3kLAN\n" +
            "cf5AcjhghL6+8hhoWpf6lk3bEnx4cImHAqGeBCN5aMN3bZqF6A3P9Q93Mm4GQd1w\n" +
            "HQKclu/T49fgqGBUnk7BhpOE9dh4LfKBdbwyK6ISaGDWrB+ogCKftCla4y5vAAAb\n" +
            "Bddp5ZjU2HLcxHALfAeymg9qlDKlj9CjKKwu5SSutWVJIn3aEJUcO/53amzh6H9N\n" +
            "dQIDAQAB\n";

    public static String aesKey = "770A8A65DA156D24"; //请随机生产
    public static String cooperator = "210775"; //商户号


//    public static String serverUrl = "http://106.14.13.223:8095/shuzu_pay/"; //测试地址
    public static String PAYMENTH5 = "http://payhub.shuzutech.com:8088/shuzu/payment/h5"; //生产地址
    public static String PAYMENTQUERY = "http://payhub.shuzutech.com:8088/shuzu/payment/query"; //生产地址

    public  String parse_response(String response){
        JsonParser parser = new JsonParser();
        JsonObject object = parser.parse(response).getAsJsonObject();

        if(object.get("reqCode").getAsString().equals("00000")) {
            String aesKey = Encrypt.rsaDecrypt(Encrypt.base64Decode(object.get("encryptKey").getAsString()), Encrypt.StringtoPrivateKey(merchantPrivateKey));
            System.out.println("aesKey:" + aesKey);
            String data = Encrypt.aesDecrypt(Encrypt.base64Decode(object.get("encryptData").getAsString()), aesKey);
            System.out.println("data:" + data);
            return data;
//            boolean check_result = Encrypt.rsaCheckSign(data, Encrypt.base64Decode(object.get("signData").getAsString()), Encrypt.StringtoPublicKey(platformPublicKey));
//            System.out.println("check_result:" + check_result);
        }
        else{
            System.out.println("resMsg:" + object.get("resMsg").getAsString());
            System.out.println("reqCode:" + object.get("reqCode").getAsString());
            throw new BizException(object.get("reqCode").getAsString(),object.get("resMsg").getAsString());
        }
    }

    public String make_request(JsonObject data,String url,String transCode){
        GsonBuilder builder;
        Gson gson;
        builder = new GsonBuilder();
        builder.disableHtmlEscaping();
        gson = builder.create();
        String json_data = gson.toJson(data);
        String encryptData = Encrypt.aesEncrypt(json_data,aesKey);

        String encryptKey = Base64.encodeBase64String(Encrypt.rsaEncrypt(aesKey,Encrypt.StringtoPublicKey(platformPublicKey)));
        String signData = Base64.encodeBase64String(Encrypt.rsaSign(json_data,Encrypt.StringtoPrivateKey(merchantPrivateKey)));

        JsonObject send = new JsonObject();
        send.addProperty("encryptData",encryptData);
        send.addProperty("encryptKey",encryptKey);
        send.addProperty("signData",signData);
        send.addProperty("reqMsgId",String.valueOf(Calendar.getInstance().getTimeInMillis()));
        send.addProperty("cooperator",cooperator);
        send.addProperty("tranCode",transCode);

        String response = HttpRequest.httpRequest(url,gson.toJson(send),"application/json");
        System.out.println(response);
        return response;
      
    }


    
    public static void main(String[] args)
    {
        test_qurery();
    }
    
    public static void test_qurery(){
        Example example = new Example();
        com.google.gson.JsonObject jsonObject = new com.google.gson.JsonObject();
        jsonObject.addProperty("merchantid",example.cooperator);
        jsonObject.addProperty("bizOrderNumber","CFD20180123122646002675");
        //jsonObject.addProperty("mid","000010000000001");
        String response = example.make_request(jsonObject,Example.PAYMENTQUERY,"SZZF005");
        String ss = example.parse_response(response);
    }
    


    public static void test_h5(){
        
        Example example = new Example();
        
        com.google.gson.JsonObject jsonObject = new com.google.gson.JsonObject();
        
        
//        商户编码  merchantid  Varchar(64) Y   商户编码
        jsonObject.addProperty("merchantid",Example.cooperator);
//        门店ID    storeId Varchar(64) N   门店ID
//        操作员ID   operatorId  Varchar(64) N   收银员ID
//        交易金额    srcAmt  Double  Y   单位圆，精确到分,如1.23
        jsonObject.addProperty("srcAmt",0.01);
//        合作方订单ID bizOrderNumber  Varchar(128)    N   不传则由后台自行生成
//        支付类型    walletType  Varchar(32) Y   1,支付宝.2, 微信
        jsonObject.addProperty("walletType",2);
//        异步通知地址  notifyUrl   Varchar(128)    Y   以http 或https 开头，商户用于接收异步通知的地址
        jsonObject.addProperty("notifyUrl","http://112.124.96.17/crawler/szcb.php");
//        用户ID    userId  VARCHAR(32) C   调用支付宝JSAPI的时候必传，支付宝用户id
//        用户openid    subOpenId   Varchar(128)    C   调用微信JSAPI支付的时候必传
        jsonObject.addProperty("subOpenId","okLGGszNCtb1VgrZmVUqYkGiNWKE");//微信用户id
//        公众号appid    subAppId    Varchar(64) C   调用微信JSAPI支付的时候必传(没有可以不传)
        jsonObject.addProperty("subAppId","wx825f2c6ff20c43a7");
//        商品描述    goods_desc  Varchar(128)    N   
//        设备号 merchantInput   Varchar(64) N   设备序列号
//        备注  memo    Varchar(1024)   N   
//        扩展参数    extParam    Varchar(1024)   N   扩展参数，以JSON格式扩展的参数，用于后续扩展。

        jsonObject.addProperty("goods_desc","");
        String response = example.make_request(jsonObject,Example.PAYMENTH5,"SZZF008");
        example.parse_response(response);
    }
}
